package bearworker.zxl.dao.mapper.extra;

import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * Created by 逍遥的熊 on 2018/1/18.
 */
public class SystemSqlProvider {

    public String getUserPriv(Map<String,Object> paramMap){
        return new SQL() {{
            SELECT("a.user_id," +
                    "f.id as menu_id," +
                    "f.menu_name," +
                    "f.url," +
                    "f.method," +
                    "f.parent_id," +
                    "f.menu_key," +
                    "f.sort," +
                    "f.is_display");
            FROM("manager_user_role a");
            FROM("manager_role b");
            FROM("manager_role_privilege c");
            FROM("manager_privilege d");
            FROM("manager_privilege_menu_entiry e");
            FROM("manager_menu f");
            WHERE("a.role_id = b.id " +
                    "and b.id = c.role_id " +
                    "and c.privi_id = d.id " +
                    "and d.id = e.privi_id " +
                    "and e.menu_id = f.id " +
                    "and a.state = 1 " +
                    "and b.state = 1 " +
                    "and c.state = 1 " +
                    "and d.state = 1 " +
                    "and e.state = 1 " +
                    "and f.state = 1 " +
                    "and f.is_display = 1 " +
                    "and f.parent_id = #{parentId}");

            if (paramMap.containsKey("userId") && paramMap.get("userId") != null) {
                WHERE(" a.user_id = #{userId}");
            }

        }}.toString();
    }

    public String getPriviDetail(){
        return new SQL() {
            {
                SELECT("b.id");
                SELECT("b.menu_name as mname");
                SELECT("b.menu_key as mkey");
                SELECT("b.state");
                FROM("manager_privilege_menu_entiry a");
                FROM("manager_menu b");
                WHERE("a.menu_id = b.id");
                WHERE("a.privi_id = #{id}");

            }
        }.toString();
    }

    public String getPriviDetail2(){
        return new SQL() {
            {
                SELECT("b.id");
                SELECT("b.entity_name as mname");
                SELECT("b.entity_key as mkey");
                SELECT("b.state");
                FROM("manager_privilege_menu_entiry a");
                FROM("manager_entity b");
                WHERE("a.entity_id = b.id");
                WHERE("a.privi_id = #{id}");

            }
        }.toString();
    }

    public String getPmenus(){
        return new SQL() {
            {
                SELECT("id,menu_name");
                FROM("manager_menu");
                WHERE("id not in (select menu_id from manager_privilege_menu_entiry where privi_id = #{priviId})");
            }
        }.toString();
    }

    public String getSmenus(){
        return new SQL() {
            {
                SELECT("id,menu_name");
                FROM("manager_menu");
                WHERE("id in (select menu_id from manager_privilege_menu_entiry where privi_id = #{priviId})");
            }
        }.toString();
    }
}
